package com.mobile.service.impl;

import com.github.pagehelper.PageInfo;
import com.mobile.service.convert.InjuryDeclarationConverter;
import com.mobile.service.dao.DaoSupport;
import com.mobile.service.dto.InjuryDeclarationDTO;
import com.mobile.service.dto.InjuryDeclarationQueryDTO;
import com.mobile.service.entity.InjuryDeclaration;
import com.mobile.service.inter.InjuryDeclarationService;
import com.mobile.service.mapper.InjuryDeclarationMapper;
import com.mobile.service.util.PageUtils;
import com.mobile.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 工伤保险申请表service实现类
 */
@Service("injuryDeclarationService")
@Transactional
public class InjuryDeclarationServiceImpl implements InjuryDeclarationService {
    @Resource(name = "daoSupport")
    private DaoSupport dao;
    @Autowired
    private InjuryDeclarationConverter injuryDeclarationConverter;
    @Autowired
    private InjuryDeclarationMapper injuryDeclarationMapper;

    @Override
    public Map<String, Object> addInjuryDeclaration(InjuryDeclarationDTO dto) throws Exception {
        Map<String, Object> map = new HashMap<>();
        
        // 查重逻辑：检查身份证号是否已存在
        InjuryDeclarationQueryDTO existingByIdNumber = new InjuryDeclarationQueryDTO();
        existingByIdNumber.setApplicantIdNumber(dto.getApplicantIdNumber());
        List<InjuryDeclarationDTO> existingList = injuryDeclarationMapper.getInjuryDeclarationList(existingByIdNumber);
        if (!existingList.isEmpty()) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0004", "该身份证号"));
            return map;
        }
        
        // 保存数据
        injuryDeclarationMapper.insertInjuryDeclaration(injuryDeclarationConverter.toEntity(dto));
        map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0011"));
        return map;
    }

    @SuppressWarnings("unchecked")
    @Override
    public PageResult<InjuryDeclarationDTO> getInjuryDeclarationListPage(InjuryDeclarationQueryDTO queryDTO) throws Exception {
        PageUtils.startPage(queryDTO);
        List<InjuryDeclarationDTO> list = injuryDeclarationMapper.injuryDeclarationlistPage(queryDTO);
        return new PageResult<InjuryDeclarationDTO>(list);
    }

    @Override
    public InjuryDeclarationDTO getInjuryDeclarationById(String id) throws Exception {
        return injuryDeclarationMapper.getInjuryDeclarationById(id);
    }

    @Override
    public Map<String, Object> updateInjuryDeclaration(InjuryDeclarationDTO dto) throws Exception {
        Map<String, Object> map = new HashMap<>();
        String pkValue = dto.getId();
        if (StringUtil.isEmpty(pkValue)) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            return map;
        }

        InjuryDeclarationDTO data = this.getInjuryDeclarationById(pkValue);
        if (Objects.isNull(data)) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            return map;
        }

        // 查重逻辑：检查身份证号是否已被其他记录使用
        InjuryDeclarationQueryDTO existingByIdNumber = new InjuryDeclarationQueryDTO();
        existingByIdNumber.setApplicantIdNumber(dto.getApplicantIdNumber());
        List<InjuryDeclarationDTO> existingList = injuryDeclarationMapper.getInjuryDeclarationList(existingByIdNumber);
        for (InjuryDeclarationDTO existing : existingList) {
            if (!existing.getId().equals(pkValue)) {
                map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
                map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0004", "该身份证号"));
                return map;
            }
        }

        injuryDeclarationMapper.updateInjuryDeclaration(injuryDeclarationConverter.toEntity(dto));
        map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0009"));
        return map;
    }

    @Override
    public Map<String, Object> removeInjuryDeclaration(String id) throws Exception {
        Map<String, Object> map = new HashMap<>();
        injuryDeclarationMapper.deleteInjuryDeclaration(id);
        map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0010"));
        return map;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<InjuryDeclarationDTO> getInjuryDeclarationList(InjuryDeclarationQueryDTO dto) throws Exception {
        return injuryDeclarationMapper.getInjuryDeclarationList(dto);
    }
}
